package com.jie.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.reggie.commen.R;
import com.jie.reggie.entity.Category;
import com.jie.reggie.entity.DishFlavor;
import com.jie.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/02/14:38
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> page1=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //升序排序
        queryWrapper.orderByAsc(Category::getSort);
        //查询
        categoryService.page(page1,queryWrapper);

        return R.success(page1);
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除id：{}",ids);
        categoryService.remove(ids);
//        categoryService.removeById(ids);
        return R.success("分类信息删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改信息{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }
    /**
     * @Description: 根据条件查询下拉框
     * @Param: [category]
     * @return: com.jie.reggie.commen.R<java.util.List<com.jie.reggie.entity.Category>>
     * @Author: 曾豪杰
     * @Date: 2022/9/2
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
